package com.fix;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedList;
import java.util.Properties;


public class DbPool {
//连接池
	private static DbPool pool;//自己的属性
	private static String diverClass;
	private static String url;
	private static String userName;
	private static String password;//连接的属性
	
	private static int intCount=3;//初始化连接
	private static int minCount=3;//最小连接数
	private static int maxCount=10;//最大连接数
	private static int count;//当前连接数
	//用集合存池子（将可用的连接放在池子里）
	LinkedList<Connection> conns =new LinkedList<Connection>();
	static {
		Properties properties=new Properties();
		InputStream ins=DbPool.class.getClassLoader().getResourceAsStream("db.properties");
		try {
			properties.load(ins);
			diverClass=properties.getProperty("jdbc.driverClass");
			System.out.println(diverClass);
			url=properties.getProperty("jdbc.url");
			System.out.println(url);
			userName=properties.getProperty("jdbc.userName");
			System.out.println(userName);
			//password=properties.getProperty("jdbc.password");
			password="";
			System.out.println(password);
			
			
			try {
				intCount=new Integer(properties.getProperty("jdbc.intCount"));
			} catch (Exception e) {}
			
			
			try {
				maxCount=new Integer(properties.getProperty("jdbc.MaxCount"));
			} catch (Exception e) {}
			
			try {
				minCount=new Integer(properties.getProperty("jdbc.MinCount"));
			} catch (Exception e) {}
			
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	//初始化连接池
	private void init () {
		//循环集合里添加连接
		for (int i=0;i<intCount;i++) {
			System.out.println("0000");
			boolean ok=conns.add(create());
		if(ok) {
			count++;
		}
		
		}
		
		System.out.println("连接池初始化完成数量为"+count);
	}
	
	//获取连接的方法
	private Connection create(){
		try {
			  Class.forName(diverClass);
		
			System.out.println("连接成功");
			return DriverManager.getConnection(url,userName,password);
		} catch (Exception e) {
			
			throw new RuntimeException("连接失败"+e.getMessage());
		}
		
		
	}
	
	//构造方法私有化
	private  DbPool() {
		init();
	}
	
	public static  DbPool getPool() {
		if(pool==null) {
			pool=new DbPool();
		}
		return pool;
	}
	
	
	
	//增加连接的方法
	private  synchronized  void autoAdd() {
		if(count == maxCount) {
			throw new RuntimeException("连接池满了");
		}
		
		for(int i=0;i<2;i++) {
			conns.add(create());
			count++;
			if(count == maxCount) {
				break;
			}
		}
		
	}
	
	
	//获取池中的连接
	public Connection getConn() {
		if(conns.size()>0) {
			return conns.removeFirst();
			
		}if(count<maxCount) {
			autoAdd();
			return getConn();
		}
		//
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return getConn();
		
	}
	
	public void sub() {
		if(conns.size()>minCount) {
			if(conns.size()>minCount) {
				try {
					Connection conn =conns.removeFirst();
					conn.close();
					conn=null;
					count--;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	
	//还回连接
	public void back(Connection c) {
		conns.add(c);
		//是否要缩减
		sub();
	}
	
	
}
